﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class BillingCredenceDA : IBillingCredenceDA
    {
        private string ACCESSORIAL_CHARGES_ID = "@ACCESSORIAL_CHARGES_ID";
        private string BILLING_AMOUNT = "@BILLING_AMOUNT";
        private string BILLING_CREDENCE_ID = "@BILLING_CREDENCE_ID";
        private string BILLING_OBJECT = "@BILLING_OBJECT";
        private string BILLING_OBJECT_ID = "@BILLING_OBJECT_ID";
        private string BILLING_OBJECT_NAME = "@BILLING_OBJECT_NAME";
        private string CHARGE_MODE = "@CHARGE_MODE";
        private string CHARGE_UNIT = "@CHARGE_UNIT";
        private string COST_TYPE = "@COST_TYPE";
        private string CREATED_BY = "@CREATED_BY";
        private string CREDENCE_TYPE = "@CREDENCE_TYPE";
        private string DOC_ID = "@DOC_ID";
        private string DOC_LINE_ID = "@DOC_LINE_ID";
        private string DOC_TYPE = "@DOC_TYPE";
        private string DUE_FROM_TO = "@DUE_FROM_TO";
        private string EXTERNAL_DOC_ID = "@EXTERNAL_DOC_ID";
        private string FIXED_RATE = "@FIXED_RATE";
        private string GROSS_WEIGHT = "@GROSS_WEIGHT";
        private string IS_STATEMENT = "@IS_STATEMENT";
        private string NET_WEIGHT = "@NET_WEIGHT";
        private string OWNER_ID = "@OWNER_ID";
        private string QUANTITY = "@QUANTITY";
        private string RATE_TYPE = "@RATE_TYPE";
        private string REMARK = "@REMARK";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_BILLING_CREDENCE_ID_EXIST = " SELECT COUNT(1) FROM @_@BILLING_CREDENCE WHERE CREDENCE_TYPE = @CREDENCE_TYPE AND DOC_ID = @DOC_ID AND DOC_TYPE = @DOC_TYPE ";
        private string SQL_CHECK_BILLING_CREDENCE_ID_STATEMENT_EXIST = " SELECT COUNT(1) FROM @_@BILLING_CREDENCE WHERE BILLING_OBJECT_ID =@BILLING_OBJECT_ID AND CREDENCE_TYPE = @CREDENCE_TYPE AND DOC_ID = @DOC_ID AND IS_STATEMENT = @IS_STATEMENT AND DOC_TYPE = @DOC_TYPE ";
        private string SQL_CHECK_BILLING_CREDENCE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@BILLING_CREDENCE WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID ";
        private string SQL_DELETE_BILLING_CREDENCE = " DELETE FROM @_@BILLING_CREDENCE WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID ";
        private string SQL_DELETE_BILLING_CREDENCE_BY_DOC_ID_TYPE = " DELETE FROM @_@BILLING_CREDENCE WHERE DOC_ID = @DOC_ID AND CREDENCE_TYPE = @CREDENCE_TYPE AND RATE_TYPE = @RATE_TYPE AND DOC_TYPE = @DOC_TYPE";
        private string SQL_INSERT_BILLING_CREDENCE = " INSERT INTO @_@BILLING_CREDENCE ( WH_ID, BILLING_CREDENCE_ID, CREDENCE_TYPE, DOC_ID, EXTERNAL_DOC_ID, DOC_TYPE, DOC_LINE_ID, ACCESSORIAL_CHARGES_ID, OWNER_ID, SKU_ID, NET_WEIGHT, VOLUME, GROSS_WEIGHT, TOTAL_NET_WEIGHT, TOTAL_VOLUME, TOTAL_GROSS_WEIGHT, BILLING_OBJECT, BILLING_OBJECT_ID, BILLING_OBJECT_NAME, RATE_TYPE, COST_TYPE, DUE_FROM_TO, CHARGE_MODE, CHARGE_UNIT, FIXED_RATE, QUANTITY, BILLING_AMOUNT, STATEMENT_ID, IS_STATEMENT, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @BILLING_CREDENCE_ID, @CREDENCE_TYPE, @DOC_ID, @EXTERNAL_DOC_ID, @DOC_TYPE, @DOC_LINE_ID, @ACCESSORIAL_CHARGES_ID, @OWNER_ID, @SKU_ID, @NET_WEIGHT, @VOLUME, @GROSS_WEIGHT, @TOTAL_NET_WEIGHT, @TOTAL_VOLUME, @TOTAL_GROSS_WEIGHT, @BILLING_OBJECT, @BILLING_OBJECT_ID, @BILLING_OBJECT_NAME, @RATE_TYPE, @COST_TYPE, @DUE_FROM_TO, @CHARGE_MODE, @CHARGE_UNIT, @FIXED_RATE, @QUANTITY, @BILLING_AMOUNT, @STATEMENT_ID, @IS_STATEMENT, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_BILLING_CREDENCE = " SELECT  BC.ROWID, BC.WH_ID, BC.BILLING_CREDENCE_ID, BC.CREDENCE_TYPE, BC.DOC_ID, BC.EXTERNAL_DOC_ID, BC.DOC_TYPE, BC.DOC_LINE_ID, BC.ACCESSORIAL_CHARGES_ID, BC.OWNER_ID, BC.SKU_ID, BC.NET_WEIGHT, BC.VOLUME, BC.GROSS_WEIGHT, BC.TOTAL_NET_WEIGHT, BC.TOTAL_VOLUME, BC.TOTAL_GROSS_WEIGHT, BC.BILLING_OBJECT, BC.BILLING_OBJECT_ID, BC.BILLING_OBJECT_NAME, BC.RATE_TYPE, BC.COST_TYPE, BC.DUE_FROM_TO, BC.CHARGE_MODE, BC.CHARGE_UNIT, BC.FIXED_RATE, BC.QUANTITY, BC.BILLING_AMOUNT, BC.STATEMENT_ID, BC.IS_STATEMENT, BC.REMARK, BC.TS, BC.CREATED_BY, CONVERT(VARCHAR(20),BC.CREATED_DATE,23) AS CREATED_DATE, BC.UPDATED_BY, BC.UPDATED_DATE, T5.[TEXT] AS CREDENCE_TYPE_DESCR,T6.[TEXT] AS DOC_TYPE_DESCR,T.[TEXT] AS BILLING_OBJECT_DESCR,T2.[TEXT] AS DUE_FROM_TO_DESCR,T3.[TEXT] AS CHARGE_UNIT_DESCR,SCD3.DESCRIPTION AS COST_TYPE_DESCR  FROM  @_@BILLING_CREDENCE BC  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = BC.BILLING_OBJECT AND SCD.SYS_CODE_TYPE = 'BILLINGOBJECT' LEFT JOIN @_@SYS_CODE_DETAIL SCD2 ON SCD2.SYS_CODE = BC.DUE_FROM_TO AND SCD2.SYS_CODE_TYPE = 'DUEFROMTO' LEFT JOIN @_@SYS_CODE_DETAIL SCD3 ON SCD3.SYS_CODE = BC.COST_TYPE AND SCD3.SYS_CODE_TYPE = 'COSTTYPE'  LEFT JOIN sysadm.TRANSLATION T ON 'BILLINGOBJECT.'+BC.BILLING_OBJECT = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T2 ON 'DUEFROMTO.'+BC.DUE_FROM_TO = T2.ITEM_ID  AND  T2.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T2.LANG+'-'+T2.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T3 ON 'CHARGEUNIT.'+BC.CHARGE_UNIT = T3.ITEM_ID  AND  T3.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T3.LANG+'-'+T3.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T4 ON 'COSTTYPE.'+BC.COST_TYPE = T4.ITEM_ID  AND  T4.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T4.LANG+'-'+T4.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T5 ON 'BillingType.'+BC.CREDENCE_TYPE = T5.ITEM_ID   AND  T5.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T5.LANG+'-'+T5.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T6 ON 'BillingType.'+BC.DOC_TYPE = T6.ITEM_ID   AND  T6.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T6.LANG+'-'+T6.COUNTRY = @LANGUAGE";
        private string SQL_SELECT_ALL_BILLING_CREDENCE_GROUP = " SELECT  BC.WH_ID, BC.CREDENCE_TYPE, BC.DOC_ID, BC.EXTERNAL_DOC_ID, BC.BILLING_OBJECT, BC.BILLING_OBJECT_ID, BC.BILLING_OBJECT_NAME, BC.DUE_FROM_TO,BC.COST_TYPE, ISNULL(BC.STATEMENT_ID,'') AS STATEMENT_ID,CASE ISNULL(BC.STATEMENT_ID,'') WHEN '' THEN 'N' ELSE 'Y' END AS IS_STATEMENT,'' AS REMARK,SUM(BC.BILLING_AMOUNT) AS BILLING_AMOUNT,  T5.[TEXT] AS CREDENCE_TYPE_DESCR,T.[TEXT] AS BILLING_OBJECT_DESCR,T2.[TEXT] AS DUE_FROM_TO_DESCR,SCD3.DESCRIPTION AS COST_TYPE_DESCR  FROM  @_@BILLING_CREDENCE BC  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = BC.BILLING_OBJECT AND SCD.SYS_CODE_TYPE = 'BILLINGOBJECT'  LEFT JOIN @_@SYS_CODE_DETAIL SCD2 ON SCD2.SYS_CODE = BC.DUE_FROM_TO AND SCD2.SYS_CODE_TYPE = 'DUEFROMTO'  LEFT JOIN @_@SYS_CODE_DETAIL SCD3 ON SCD3.SYS_CODE = BC.COST_TYPE AND SCD3.SYS_CODE_TYPE = 'COSTTYPE'  LEFT JOIN sysadm.TRANSLATION T ON 'BILLINGOBJECT.'+BC.BILLING_OBJECT = T.ITEM_ID   AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE  LEFT JOIN sysadm.TRANSLATION T2 ON 'DUEFROMTO.'+BC.DUE_FROM_TO = T2.ITEM_ID  AND  T2.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T2.LANG+'-'+T2.COUNTRY = @LANGUAGE  LEFT JOIN sysadm.TRANSLATION T3 ON 'CHARGEUNIT.'+BC.CHARGE_UNIT = T3.ITEM_ID  AND  T3.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T3.LANG+'-'+T3.COUNTRY = @LANGUAGE  LEFT JOIN sysadm.TRANSLATION T4 ON 'COSTTYPE.'+BC.COST_TYPE = T4.ITEM_ID  AND  T4.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T4.LANG+'-'+T4.COUNTRY = @LANGUAGE  LEFT JOIN sysadm.TRANSLATION T5 ON 'BillingType.'+BC.CREDENCE_TYPE = T5.ITEM_ID   AND  T5.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T5.LANG+'-'+T5.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T6 ON 'BillingType.'+BC.DOC_TYPE = T6.ITEM_ID   AND  T6.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T6.LANG+'-'+T6.COUNTRY = @LANGUAGE @WHERE  GROUP BY BC.WH_ID, BC.CREDENCE_TYPE, BC.DOC_ID, BC.EXTERNAL_DOC_ID, BC.BILLING_OBJECT, BC.BILLING_OBJECT_ID, BC.BILLING_OBJECT_NAME, BC.DUE_FROM_TO,BC.COST_TYPE,ISNULL(BC.STATEMENT_ID,''), T.[TEXT],T2.[TEXT],SCD3.DESCRIPTION,T5.[TEXT]";
        private string SQL_UPDATE_BILLING_CREDENCE = " UPDATE @_@BILLING_CREDENCE SET WH_ID = @WH_ID, CREDENCE_TYPE = @CREDENCE_TYPE, DOC_ID = @DOC_ID, EXTERNAL_DOC_ID = @EXTERNAL_DOC_ID, DOC_TYPE = @DOC_TYPE, DOC_LINE_ID = @DOC_LINE_ID, ACCESSORIAL_CHARGES_ID = @ACCESSORIAL_CHARGES_ID, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, NET_WEIGHT = @NET_WEIGHT, VOLUME = @VOLUME, GROSS_WEIGHT = @GROSS_WEIGHT, TOTAL_NET_WEIGHT = @TOTAL_NET_WEIGHT, TOTAL_VOLUME = @TOTAL_VOLUME, TOTAL_GROSS_WEIGHT = @TOTAL_GROSS_WEIGHT, BILLING_OBJECT = @BILLING_OBJECT, BILLING_OBJECT_ID = @BILLING_OBJECT_ID, BILLING_OBJECT_NAME = @BILLING_OBJECT_NAME, RATE_TYPE = @RATE_TYPE, COST_TYPE = @COST_TYPE, DUE_FROM_TO = @DUE_FROM_TO, CHARGE_MODE = @CHARGE_MODE, CHARGE_UNIT = @CHARGE_UNIT, FIXED_RATE = @FIXED_RATE, QUANTITY = @QUANTITY, BILLING_AMOUNT = @BILLING_AMOUNT, STATEMENT_ID = @STATEMENT_ID, IS_STATEMENT = @IS_STATEMENT, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID  ";
        private string SQL_UPDATE_BILLING_CREDENCE_PRINT = " UPDATE @_@BILLING_CREDENCE SET PRINTED_DATE = @PRINTED_DATE,UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID";
        private string SQL_UPDATE_BILLING_CREDENCE_STATEMENT = " UPDATE @_@BILLING_CREDENCE SET STATEMENT_ID = @STATEMENT_ID, IS_STATEMENT = @IS_STATEMENT WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID ";
        private string STATEMENT_ID = "@STATEMENT_ID";
        private string TOTAL_GROSS_WEIGHT = "@TOTAL_GROSS_WEIGHT";
        private string TOTAL_NET_WEIGHT = "@TOTAL_NET_WEIGHT";
        private string TOTAL_VOLUME = "@TOTAL_VOLUME";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VOLUME = "@VOLUME";
        private string WH_ID = "@WH_ID";

        public bool CheckBillingCredenceByTypeAndID(string credenceType, string docID, string docType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@DOC_ID", docID), new SqlParameter("@DOC_TYPE", docType) };
            if (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_EXIST.Replace("@_@", whLoginID), paras).ToString() == "0")
            {
                return false;
            }
            return true;
        }

        public bool CheckBillingCredenceByTypeAndID(DataBase dataBase, DbTransaction tran, string credenceType, string docID, string docType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@DOC_ID", docID), new SqlParameter("@DOC_TYPE", docType) };
            if (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_EXIST.Replace("@_@", whLoginID), paras).ToString() == "0")
            {
                return false;
            }
            return true;
        }

        public bool CheckBillingCredenceIDUnique(string billingCredenceID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckBillingCredenceIDUnique(DataBase dataBase, DbTransaction tran, string billingCredenceID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckBillingCredenceStatementByTypeAndID(string billingObjectID, string credenceType, string docID, string statement, string docType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_OBJECT_ID", billingObjectID), new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@DOC_ID", docID), new SqlParameter("@IS_STATEMENT", statement), new SqlParameter("@DOC_TYPE", docType) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_STATEMENT_EXIST.Replace("@_@", whLoginID), paras).ToString() != "0");
        }

        public bool CheckBillingCredenceStatementByTypeAndID(DataBase dataBase, DbTransaction tran, string billingObjectID, string credenceType, string docID, string statement, string docType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_OBJECT_ID", billingObjectID), new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@DOC_ID", docID), new SqlParameter("@IS_STATEMENT", statement), new SqlParameter("@DOC_TYPE", docType) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BILLING_CREDENCE_ID_STATEMENT_EXIST.Replace("@_@", whLoginID), paras).ToString() != "0");
        }

        public int DeleteBillingCredence(List<string> billingCredenceID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < billingCredenceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteBillingCredence(DataBase dataBase, DbTransaction tran, List<string> billingCredenceID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < billingCredenceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteBillingCredence(string docID, string credenceType, string rateType, string docType, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOC_ID", docID), new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@RATE_TYPE", rateType), new SqlParameter("@DOC_TYPE", docType) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_CREDENCE_BY_DOC_ID_TYPE.Replace("@_@", whLoginID), paras);
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteBillingCredence(DataBase dataBase, DbTransaction tran, string docID, string credenceType, string rateType, string docType, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DOC_ID", docID), new SqlParameter("@CREDENCE_TYPE", credenceType), new SqlParameter("@RATE_TYPE", rateType), new SqlParameter("@DOC_TYPE", docType) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_CREDENCE_BY_DOC_ID_TYPE.Replace("@_@", whLoginID), paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllBillingCredence(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllBillingCredence(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public BillingCredenceInfo GetBillingCredenceByID(string billingcredenceID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + " WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingcredenceID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            BillingCredenceInfo billingCredenceInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return billingCredenceInfo;
        }

        public BillingCredenceInfo GetBillingCredenceByID(DataBase dataBase, DbTransaction tran, string billingcredenceID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + " WHERE BILLING_CREDENCE_ID = @BILLING_CREDENCE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@BILLING_CREDENCE_ID", billingcredenceID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            BillingCredenceInfo billingCredenceInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return billingCredenceInfo;
        }

        public DataSet GetBillingCredenceByQueryList(List<string> sqlWhere, BillingCredenceQueryEntity billingcredenceQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!billingcredenceQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingcredenceQuery.CurrentPage, billingcredenceQuery.PageSize, billingcredenceQuery.SortField, billingcredenceQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetBillingCredenceByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, BillingCredenceQueryEntity billingcredenceQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!billingcredenceQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingcredenceQuery.CurrentPage, billingcredenceQuery.PageSize, billingcredenceQuery.SortField, billingcredenceQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public DataSet GetBillingCredenceByStatementID(string statementID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = " WHERE STATEMENT_ID = @STATEMENT_ID ";
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE_GROUP.Replace("@_@", whLoginID).Replace("@WHERE", temp);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql, paras);
        }

        public DataSet GetBillingCredenceByStatementID(DataBase dataBase, DbTransaction tran, string statementID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = " WHERE STATEMENT_ID = @STATEMENT_ID ";
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE_GROUP.Replace("@_@", whLoginID).Replace("@WHERE", temp);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql, paras);
        }

        public DataSet GetBillingCredenceGroupByQueryList(List<string> sqlWhere, BillingCredenceQueryEntity billingcredenceQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = string.Empty;
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE_GROUP.Replace("@_@", whLoginID).Replace("@WHERE", temp);
            if (!billingcredenceQuery.IsGetAll)
            {
                sql = PagingHelper.GetPagingSQL(temp, billingcredenceQuery.CurrentPage, billingcredenceQuery.PageSize, billingcredenceQuery.SortField, billingcredenceQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                sql = sql.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetBillingCredenceGroupByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, BillingCredenceQueryEntity billingcredenceQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = string.Empty;
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE_GROUP.Replace("@_@", whLoginID).Replace("@WHERE", temp);
            if (!billingcredenceQuery.IsGetAll)
            {
                sql = PagingHelper.GetPagingSQL(temp, billingcredenceQuery.CurrentPage, billingcredenceQuery.PageSize, billingcredenceQuery.SortField, billingcredenceQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                sql = sql.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<BillingCredenceInfo> GetBillingCredenceListByCondition(BillingCredenceInfo obj, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + "  WHERE (CASE ISNULL(IS_STATEMENT,'') WHEN 'Y' THEN 'Y' ELSE 'N' END) = @IS_STATEMENT AND ISNULL(CREDENCE_TYPE,'') = @CREDENCE_TYPE AND ISNULL(DOC_ID,'') = @DOC_ID AND ISNULL(BILLING_OBJECT,'') = @BILLING_OBJECT  AND ISNULL(BILLING_OBJECT_ID,'') = @BILLING_OBJECT_ID AND ISNULL(DUE_FROM_TO,'') = @DUE_FROM_TO AND ISNULL(COST_TYPE,'') = @COST_TYPE  AND ISNULL(STATEMENT_ID,'') = @STATEMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage), new SqlParameter("@IS_STATEMENT", obj.IsStatement), new SqlParameter("@CREDENCE_TYPE", obj.CredenceType), new SqlParameter("@DOC_ID", obj.DocID), new SqlParameter("@BILLING_OBJECT", obj.BillingObject), new SqlParameter("@BILLING_OBJECT_ID", obj.BillingObjectID), new SqlParameter("@DUE_FROM_TO", obj.DueFromTo), new SqlParameter("@COST_TYPE", obj.CostType), new SqlParameter("@STATEMENT_ID", obj.StatementID) };
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            BillingCredenceInfo billingCredenceInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    billingCredenceListInfo.Add(billingCredenceInfo);
                }
            }
            return billingCredenceListInfo;
        }

        public List<BillingCredenceInfo> GetBillingCredenceListByCondition(DataBase dataBase, DbTransaction tran, BillingCredenceInfo obj, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + "  WHERE (CASE ISNULL(IS_STATEMENT,'') WHEN 'Y' THEN 'Y' ELSE 'N' END) = @IS_STATEMENT AND ISNULL(CREDENCE_TYPE,'') = @CREDENCE_TYPE AND ISNULL(DOC_ID,'') = @DOC_ID AND ISNULL(BILLING_OBJECT,'') = @BILLING_OBJECT  AND ISNULL(BILLING_OBJECT_ID,'') = @BILLING_OBJECT_ID AND ISNULL(DUE_FROM_TO,'') = @DUE_FROM_TO AND ISNULL(COST_TYPE,'') = @COST_TYPE  AND ISNULL(STATEMENT_ID,'') = @STATEMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage), new SqlParameter("@IS_STATEMENT", obj.IsStatement), new SqlParameter("@CREDENCE_TYPE", obj.CredenceType), new SqlParameter("@DOC_ID", obj.DocID), new SqlParameter("@BILLING_OBJECT", obj.BillingObject), new SqlParameter("@BILLING_OBJECT_ID", obj.BillingObjectID), new SqlParameter("@DUE_FROM_TO", obj.DueFromTo), new SqlParameter("@COST_TYPE", obj.CostType), new SqlParameter("@STATEMENT_ID", obj.StatementID) };
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            BillingCredenceInfo billingCredenceInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                billingCredenceListInfo.Add(billingCredenceInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return billingCredenceListInfo;
        }

        public List<BillingCredenceInfo> GetBillingCredenceListByWhere(string where, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + where;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            BillingCredenceInfo billingCredenceInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    billingCredenceListInfo.Add(billingCredenceInfo);
                }
            }
            return billingCredenceListInfo;
        }

        public List<BillingCredenceInfo> GetBillingCredenceListByWhere(DataBase dataBase, DbTransaction tran, string where, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_CREDENCE.Replace("@_@", whLoginID) + where;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            BillingCredenceInfo billingCredenceInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                billingCredenceInfo = new BillingCredenceInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["BILLING_CREDENCE_ID"].ToString(), reader["CREDENCE_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), (reader["DOC_LINE_ID"].ToString() != "") ? int.Parse(reader["DOC_LINE_ID"].ToString()) : 0, reader["ACCESSORIAL_CHARGES_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["NET_WEIGHT"].ToString()) : 0M, (reader["VOLUME"].ToString() != "") ? decimal.Parse(reader["VOLUME"].ToString()) : 0M, (reader["GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["GROSS_WEIGHT"].ToString()) : 0M, (reader["TOTAL_NET_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_NET_WEIGHT"].ToString()) : 0M, (reader["TOTAL_VOLUME"].ToString() != "") ? decimal.Parse(reader["TOTAL_VOLUME"].ToString()) : 0M, (reader["TOTAL_GROSS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["TOTAL_GROSS_WEIGHT"].ToString()) : 0M, reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["RATE_TYPE"].ToString(), reader["COST_TYPE"].ToString(), reader["DUE_FROM_TO"].ToString(), reader["CHARGE_MODE"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["FIXED_RATE"].ToString() != "") ? decimal.Parse(reader["FIXED_RATE"].ToString()) : 0M, (reader["QUANTITY"].ToString() != "") ? decimal.Parse(reader["QUANTITY"].ToString()) : 0M, (reader["BILLING_AMOUNT"].ToString() != "") ? decimal.Parse(reader["BILLING_AMOUNT"].ToString()) : 0M, reader["STATEMENT_ID"].ToString(), reader["IS_STATEMENT"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                billingCredenceListInfo.Add(billingCredenceInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return billingCredenceListInfo;
        }

        public int InsertBillingCredence(List<BillingCredenceInfo> billingCredenceListInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < billingCredenceListInfo.Count; i++)
                {
                    billingCredenceListInfo[i].WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceListInfo[i]);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_CREDENCE.Replace("@_@", whLoginID), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertBillingCredence(BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertBillingCredence(DataBase dataBase, DbTransaction tran, List<BillingCredenceInfo> billingCredenceListInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < billingCredenceListInfo.Count; i++)
                {
                    billingCredenceListInfo[i].WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceListInfo[i]);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_CREDENCE.Replace("@_@", whLoginID), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertBillingCredence(DataBase dataBase, DbTransaction tran, BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_BillingCredence_Parameters(BillingCredenceInfo billingCredenceInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.BILLING_CREDENCE_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CREDENCE_TYPE, SqlDbType.VarChar, 30), new SqlParameter(this.DOC_ID, SqlDbType.VarChar, 30), new SqlParameter(this.EXTERNAL_DOC_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DOC_TYPE, SqlDbType.VarChar, 30), new SqlParameter(this.DOC_LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.ACCESSORIAL_CHARGES_ID, SqlDbType.VarChar, 30), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.NET_WEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.VOLUME, SqlDbType.Decimal, 13), new SqlParameter(this.GROSS_WEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.TOTAL_NET_WEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.TOTAL_VOLUME, SqlDbType.Decimal, 13), new SqlParameter(this.TOTAL_GROSS_WEIGHT, SqlDbType.Decimal, 13), 
                new SqlParameter(this.BILLING_OBJECT, SqlDbType.VarChar, 30), new SqlParameter(this.BILLING_OBJECT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.BILLING_OBJECT_NAME, SqlDbType.VarChar, 50), new SqlParameter(this.RATE_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.COST_TYPE, SqlDbType.VarChar, 50), new SqlParameter(this.DUE_FROM_TO, SqlDbType.VarChar, 10), new SqlParameter(this.CHARGE_MODE, SqlDbType.VarChar, 20), new SqlParameter(this.CHARGE_UNIT, SqlDbType.VarChar, 20), new SqlParameter(this.FIXED_RATE, SqlDbType.Decimal, 13), new SqlParameter(this.QUANTITY, SqlDbType.Decimal, 13), new SqlParameter(this.BILLING_AMOUNT, SqlDbType.Decimal, 13), new SqlParameter(this.STATEMENT_ID, SqlDbType.VarChar, 30), new SqlParameter(this.IS_STATEMENT, SqlDbType.VarChar, 1), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30)
             };
            paramArray[0].Value = billingCredenceInfo.WhID;
            paramArray[1].Value = billingCredenceInfo.BillingCredenceID;
            paramArray[2].Value = billingCredenceInfo.CredenceType;
            paramArray[3].Value = billingCredenceInfo.DocID;
            if (!string.IsNullOrEmpty(billingCredenceInfo.ExternalDocID))
            {
                paramArray[4].Value = billingCredenceInfo.ExternalDocID;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = billingCredenceInfo.DocType;
            paramArray[6].Value = billingCredenceInfo.DocLineID;
            if (!string.IsNullOrEmpty(billingCredenceInfo.AccessorialChargesID))
            {
                paramArray[7].Value = billingCredenceInfo.AccessorialChargesID;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.OwnerID))
            {
                paramArray[8].Value = billingCredenceInfo.OwnerID;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.SkuID))
            {
                paramArray[9].Value = billingCredenceInfo.SkuID;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            paramArray[10].Value = billingCredenceInfo.NetWeight;
            paramArray[11].Value = billingCredenceInfo.Volume;
            paramArray[12].Value = billingCredenceInfo.GrossWeight;
            paramArray[13].Value = billingCredenceInfo.TotalNetWeight;
            paramArray[14].Value = billingCredenceInfo.TotalVolume;
            paramArray[15].Value = billingCredenceInfo.TotalGrossWeight;
            paramArray[0x10].Value = billingCredenceInfo.BillingObject;
            paramArray[0x11].Value = billingCredenceInfo.BillingObjectID;
            if (!string.IsNullOrEmpty(billingCredenceInfo.BillingObjectName))
            {
                paramArray[0x12].Value = billingCredenceInfo.BillingObjectName;
            }
            else
            {
                paramArray[0x12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.RateType))
            {
                paramArray[0x13].Value = billingCredenceInfo.RateType;
            }
            else
            {
                paramArray[0x13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.CostType))
            {
                paramArray[20].Value = billingCredenceInfo.CostType;
            }
            else
            {
                paramArray[20].Value = DBNull.Value;
            }
            paramArray[0x15].Value = billingCredenceInfo.DueFromTo;
            if (!string.IsNullOrEmpty(billingCredenceInfo.ChargeMode))
            {
                paramArray[0x16].Value = billingCredenceInfo.ChargeMode;
            }
            else
            {
                paramArray[0x16].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.ChargeUnit))
            {
                paramArray[0x17].Value = billingCredenceInfo.ChargeUnit;
            }
            else
            {
                paramArray[0x17].Value = DBNull.Value;
            }
            paramArray[0x18].Value = billingCredenceInfo.FixedRate;
            paramArray[0x19].Value = billingCredenceInfo.Quantity;
            paramArray[0x1a].Value = billingCredenceInfo.BillingAmount;
            if (!string.IsNullOrEmpty(billingCredenceInfo.StatementID))
            {
                paramArray[0x1b].Value = billingCredenceInfo.StatementID;
            }
            else
            {
                paramArray[0x1b].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.IsStatement))
            {
                paramArray[0x1c].Value = billingCredenceInfo.IsStatement;
            }
            else
            {
                paramArray[0x1c].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingCredenceInfo.Remark))
            {
                paramArray[0x1d].Value = billingCredenceInfo.Remark;
            }
            else
            {
                paramArray[0x1d].Value = DBNull.Value;
            }
            paramArray[30].Value = billingCredenceInfo.CreatedBy;
            paramArray[0x1f].Value = billingCredenceInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateBillingCredence(BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingCredence(DataBase dataBase, DbTransaction tran, BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingCredence_Parameters(billingCredenceInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingCredenceByPrint(BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINTED_DATE", billingCredenceInfo.PrintedDate), new SqlParameter("@UPDATED_BY", billingCredenceInfo.UpdatedBy), new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceInfo.BillingCredenceID) };
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE_PRINT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingCredenceByPrint(DataBase dataBase, DbTransaction tran, BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINTED_DATE", billingCredenceInfo.PrintedDate), new SqlParameter("@UPDATED_BY", billingCredenceInfo.UpdatedBy), new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceInfo.BillingCredenceID) };
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE_PRINT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingCredenceByStatement(BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", billingCredenceInfo.StatementID), new SqlParameter("@IS_STATEMENT", billingCredenceInfo.IsStatement), new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceInfo.BillingCredenceID) };
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE_STATEMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingCredenceByStatement(DataBase dataBase, DbTransaction tran, BillingCredenceInfo billingCredenceInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingCredenceInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", billingCredenceInfo.StatementID), new SqlParameter("@IS_STATEMENT", billingCredenceInfo.IsStatement), new SqlParameter("@BILLING_CREDENCE_ID", billingCredenceInfo.BillingCredenceID) };
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_CREDENCE_STATEMENT.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

